use egui::{Id, Pos2};
use QcUI::{
    component::{UiNode, Widget},
    core::context::UiContext,
};

pub trait UiNodeExt {
    fn hit_test(&self, point: Pos2) -> bool {
        false
    }

    fn render_from_ui_panel(&mut self, ctx: &mut UiContext) {}
}

impl UiNodeExt for Widget {
    fn hit_test(&self, point: Pos2) -> bool {
        self.get_rect().contains(point)
    }
}

impl UiNodeExt for UiNode {
    fn render_from_ui_panel(&mut self, ctx: &mut UiContext) {
        let widget = self.get_widget();
        let area = egui::Area::new(Id::new(&widget.name)).fixed_pos(widget.get_rect().min);
        let context = ctx.ui.ctx().clone();
        area.show(&context, |ui| {
            self.render(ctx);
        });
    }
}
